import { getTaskInstance } from "src/config";
import { AddToQueueConfig } from '../types/index';
import { queueManager } from '../manager/index';

export default function AddToQueue(config: AddToQueueConfig) {
  return (target, key, descriptor) => {
    const ins = getTaskInstance(target.constructor)
    const originFn = descriptor.value
    descriptor.value = async (...args) => {
      let result = await originFn.apply(ins, args)
      if (!!result) {
        result = result.constructor == Array ? result : [result]
        queueManager.addToQueue({
          queueName: config.name,
          urls: result
        })
      }
      return result
    }
    return descriptor
  }
}